<?php
class PNCarProperty extends PNObjectEx {
  function PNCarProperty($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'innoforum_carproperty';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_init($init, $where);
  }

  function validate(){
    $form = FormUtil::getPassedValue ('form', false);
    if (empty($form['name'])){
      $message[] = 'field name must not be empty.';
      $this->setError($message);
      return false;
    }
    $this->clearError();
    return true;
  }


  function insertPostProcess(){

    $sb = FormUtil::getPassedValue ('sb', false);
    $id = $this->_objData['id'];

    if($sb && $id){
      foreach($sb as $key=>$val ){
        //$valArray = explode("@", $key);
        $obj[] = array('category_id'    => $val,
                       'subcategory_id' => $key,
                       'carproperty_id'       => $id
                      );
      }
      //Do the insert
      DBUtil::insertObjectArray($obj, 'innoforum_subcategory_carproperty');
    } 


  }
/*
  function updatePostProcess(){

    $id = $this->_objData['id'];
    $sbFormArray = FormUtil::getPassedValue ('sb', false);

    if($id && $sbFormArray){
      $sbDBArray = DBUtil::selectObjectArray( 'innoforum_subcategory_carproperty', 
                                                                "WHERE spro_carproperty_id = '$id'",
                                                                '',
                                                                -1,
                                                                -1,
                                                                '',
                                                                null,
                                                                null,
                                                                array( 'id', 
                                                                      'category_id', 
                                                                      'subcategory_id', 
                                                                      'carproperty_id'
                                                                    )
                                                                );
      //var_dump($sbFormArray);
      //var_dump($sbDBArray);

      if(sizeof($sbDBArray)>0){
        foreach($sbFormArray as $keyForm=>$valForm ){
          $compare = false;
          foreach($sbDBArray as $keyDB=>$valDB ){
            if($keyForm == $valDB['subcategory_id']){
              $compare = true;
              $res[$keyForm] = 'equal';
            }
          }
          if($compare == false) {
            //Insert state when exist data
            //$res[$keyForm] = 'not equal';
            $obj = array('category_id'    => $valForm,
                           'subcategory_id' => $keyForm,
                           'carproperty_id'       => $id
                          );
            //Do the insert
            DBUtil::insertObject($obj, 'innoforum_subcategory_carproperty');
          }
        }
      }else{
        //Insert new data statement when data is empty
        foreach($sbFormArray as $keyForm=>$valForm ){
          $obj = array('category_id'    => $valForm,
                           'subcategory_id' => $keyForm,
                           'carproperty_id'       => $id
                          );
          //Do the insert
          DBUtil::insertObject($obj, 'innoforum_subcategory_carproperty');
        }
      }
      //var_dump($res); exit; 

      foreach($sbDBArray as $keyDB=>$valDB ){
        $compare = false;
        foreach($sbFormArray as $keyForm=>$valForm ){
          if($keyForm == $valDB['subcategory_id']){
            $compare = true;
            $db[$valDB['subcategory_id']] = 'equal';
          }
        }
        if($compare == false) {
            //Delete state when database exist
            //$db[$valDB['subcategory_id']] = 'not equal';
            DBUtil::deleteObjectByID( 'innoforum_subcategory_carproperty', $valDB['id'], 'id');
        }
      }
    }
    //var_dump($db); exit;
  }
*/


  function updatePostProcess(){

    $id = $this->_objData['id'];
    $carproperty_id = $this->_objData['carproperty_id'];
    $sbFormArray = FormUtil::getPassedValue ('sb', false);

    //Delete old data 
    DBUtil::deleteObjectByID( 'innoforum_subcategory_carproperty', $id, 'carproperty_id');

    //Insert new data statement when data is empty
    foreach($sbFormArray as $keyForm=>$valForm ){
      $obj = array('category_id'    => $valForm,
                       'subcategory_id' => $keyForm,
                       'carproperty_id'       => $id
                      );
      //Do the insert
      DBUtil::insertObject($obj, 'innoforum_subcategory_carproperty');
    }
  }

  function deletePostProcess(){
    $id = $this->_objData['id'];
    //Delete old data 
    DBUtil::deleteObjectByID('innoforum_subcategory_carproperty', $id, 'carproperty_id');
  }

  function selectExtendResult(){
    $id = FormUtil::getPassedValue ('id', false);
    $result = array();
    if ($id){

      $result['subcategory_carproperty'] = DBUtil::selectObjectArray( 'innoforum_subcategory_carproperty', 
                                                                "WHERE spro_carproperty_id = '$id'",
                                                                '',
                                                                -1,
                                                                -1,
                                                                '',
                                                                null,
                                                                null,
                                                                array( 'id', 
                                                                      'category_id', 
                                                                      'subcategory_id', 
                                                                      'carproperty_id'
                                                                    )
                                                                );

    }
    return $result;
  }

}